package com.health.redis;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;

import com.health.mapp.utils.PropertiesUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 取redis 连接池 <一句话功能简述> <功能详细描述>
 * 
 * @author khj
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class RedisClientPool {
	
	private static Logger logger = Logger.getLogger(RedisClientPool.class);
	public static RedisClientPool redisClientPool = getInstance();

	public static JedisPool jedisPool;
	private static Jedis jedis;
	public static boolean jedisIsConnected = false;
	static String host = "";// "localhost";
	static int port = 6379;// 6379;
	static int timeout = 60000;// 60000;

	static String  passwd="";
	public static int maxTotal=0;
	public static int maxIdle=0;
	public static int maxWaitMillis=0;

	public static synchronized RedisClientPool getInstance() {
		if (null == redisClientPool) {
			redisClientPool = new RedisClientPool();
		}
		return redisClientPool;
	}

	public RedisClientPool() {
		if (null == jedisPool) {
			init();
		}
	}

	/**
	 * 初始化Jedis <一句话功能简述> <功能详细描述>
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	private static JedisPoolConfig initPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		// 控制一个pool最多有多少个状态为idle的jedis实例
		jedisPoolConfig.setMaxIdle(Integer.parseInt(PropertiesUtil
				.getString("Jedis.client.pool.maxIdle")));
		// 最大能够保持空闲状态的对象数
		// jedisPoolConfig.setMaxIdle(Integer.parseI
		// nt(PropertiesUtil.getString("Jedis.client.pool.maxWait")));
		// 最大能够保持空闲状态的对象数
		// jedisPoolConfig.setMaxIdle(Integer.parseInt(PropertiesUtil.getString("Jedis.client.pool.maxActive")));
		// 超时时间
		jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(PropertiesUtil
				.getString("Jedis.client.pool.expire")));
		// 在borrow一个jedis实例时，是否提前进行alidate操作；如果为true，则得到的jedis实例均是可用的；
		jedisPoolConfig.setTestOnBorrow(true);
		// 在还会给pool时，是否提前进行validate操作
		jedisPoolConfig.setTestOnReturn(true);

		return jedisPoolConfig;
	}

	/**
	 * 初始化jedis连接池
	 */
	public static void init() {
	 
			//System.out.println("" + jedisPool					+ Thread.currentThread().getName());
			JedisPoolConfig jedisPoolConfig = initPoolConfig();
			host = PropertiesUtil.getString("Jedis.server.host");// "localhost";
			port = Integer.parseInt(PropertiesUtil
					.getString("Jedis.server.port"));// 6379;
			passwd=PropertiesUtil.getString("Jedis.server.password");
			timeout = Integer.parseInt(PropertiesUtil
					.getString("Jedis.client.pool.expire"));
			
			logger.debug("RedisClientPool host:" + host);
			logger.debug(" RedisClientPoolport:" + port);
			//System.out.println("passwd" + passwd);
			logger.debug(" RedisClientPool timeout:" + timeout);
			if("".equals(passwd)){

				passwd= null;

			}
			// 构造连接池
			jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,passwd);
		 
			//System.out.println("" + jedisPool 					+ Thread.currentThread().getName());
	}

	public static boolean jedisTextX() {
		try {

			host = PropertiesUtil.getString("Jedis.server.host");// "localhost";
			port = Integer.parseInt(PropertiesUtil
					.getString("Jedis.server.port"));// 6379;
			timeout = Integer.parseInt(PropertiesUtil
					.getString("Jedis.client.pool.expire"));// 60000;

			logger.debug("host:" + host);
			// 连接redis服务器，192.168.0.100:6379
			jedis = new Jedis(host, port);
			if (null != jedis) {
				// connect
				jedis.connect();
				// 权限认证
				// jedis.auth("admin");
				jedisIsConnected = jedis.isConnected();
				logger.debug("jedis isConnected:" + jedisIsConnected);
				jedis.disconnect();
			}

		} catch (Exception e) {

			e.printStackTrace();
			jedisIsConnected = jedis.isConnected();
			logger.error("jedis isConnected:" + jedisIsConnected);
			jedis.disconnect();
		}
		return jedisIsConnected;
	}

	public static Jedis getJedis() {
		try {

			host = PropertiesUtil.getString("Jedis.server.host");// "localhost";
			port = Integer.parseInt(PropertiesUtil
					.getString("Jedis.server.port"));// 6379;
			timeout = Integer.parseInt(PropertiesUtil
					.getString("Jedis.client.pool.expire"));// 60000;

			logger.debug("host:" + host);
			// 连接redis服务器，192.168.0.100:6379
			jedis = new Jedis(host, port);
			if (null != jedis) {
				// connect
				jedis.connect();
				// 权限认证
				// jedis.auth("admin");
				jedisIsConnected = jedis.isConnected();
				logger.debug("jedis isConnected:" + jedisIsConnected);
				// jedis.disconnect();
			}

		} catch (Exception e) {

			e.printStackTrace();
			jedisIsConnected = jedis.isConnected();
			logger.error("jedis isConnected:" + jedisIsConnected);
			jedis.disconnect();
		}
		return jedis;
	}

	public static void closeJedis() {
		if (null != jedis) {
			// connect
			jedis.connect();
			// 权限认证
			// jedis.auth("admin");

			jedis.disconnect();
			jedisIsConnected = jedis.isConnected();
			logger.debug("jedis isConnected:" + jedisIsConnected);
		}
	}
}
